﻿@page "/monthlyinvvaluationtable"

@inject INavInvValuationService NavInvValuationService

<PageTitle>Monthly Inv Valuation Table</PageTitle>
<div style="position: sticky; top: 3.5rem; z-index: 1;background-color:White">
    <div class="row position-relative">
        <div class="col input-group mb-2">
            <span class="input-group-text">搜索</span>
            <input class="form-control" placeholder="Search"
                   @bind-value="@_search" @oninput="@Search" />
        </div>
        <div class="col form-check ms-3 mt-2">
            <input class="form-check-input"
                   type="checkbox"
                   value="@_valuation"
                   @onchange="OnValuationChange">
            <label class="form-check-label">
                切换显示月平均消耗和入库
            </label>
        </div>
        <div class="col form-check ms-3 mt-2">
            <input class="form-check-input"
                   type="checkbox"
                   value="@_showMonthDetails"
                   @onchange="OnShowMonthDetailsChange">
            <label class="form-check-label">
                切换显示近12月明细（红色消耗，绿色入库）
            </label>
        </div>
        <div class="col mt-2">
            @if (FilterFsMonthlyValuationDtos.Count != 0)
            {
                <span>总行数:@FilterFsMonthlyValuationDtos.Count</span>
            }
        </div>
    </div>
</div>
<table class="table table-hover table-striped" style="table-layout: fixed; word-break: break-all;">
    <thead style="position: sticky; top: 6rem; z-index: 1; background-color: White">
        <tr>
            <th style="width: 120px;">Number</th>
            <th style="width: 15%;">Description</th>
            @if (!_valuation)
            {
                <th style="width: 40px;">BU</th>
                <th>Unit</th>
                <th>UnitCost</th>
                <th>Inventory</th>
                <th>InvValue</th>
                <th>SafetyStock</th>
                <th>SsValue</th>
                <th>OpenPo</th>
                <th>OPValue</th>
                <th>MTPT</th>
            }
            else
            {
                <th style="color: red">ConsFreq</th>
                <th style="color: red">12ConsAvg</th>
                <th style="color: red">12VConsAvg</th>
                <th style="color: red">6ConsAvg</th>
                <th style="color: red">3ConsAvg</th>

                <th style="color: green">RecvFreq</th>
                <th style="color: green">12RecvAvg</th>
                <th style="color: green">12VRecvAvg</th>
                <th style="color: green">6RecvAvg</th>
                <th style="color: green">3RecvAvg</th>
            }
        </tr>
    </thead>
    <tbody>
        @if (FsMonthlyValuationDtos.Count == 0)
        {
            <tr>
                <td colspan="12">请稍候，正在查询数据...</td>
            </tr>
        }
        else
        {
            @foreach (var dto in FilterFsMonthlyValuationDtos)
            {
                <tr>
                    <td>@dto.Number</td>
                    <td style="text-overflow: ellipsis; overflow: hidden; white-space: nowrap">@dto.Description</td>
                    @if (!_valuation)
                    {
                        <td>@dto.Sbu</td>
                        <td>@dto.Unit</td>
                        <td>@dto.UnitCost</td>
                        <td>@dto.Inventory</td>
                        <td>@dto.InvValue</td>
                        <td>@dto.SafetyStock</td>
                        <td>@dto.SsValue</td>
                        <td>@dto.OpenPo</td>
                        <td>@dto.OpenPoValue</td>
                        <td>@dto.MTPT</td>
                    }
                    else
                    {
                        <td style="color: red">@dto.ConsFrequency</td>
                        <td style="color: red">@dto.ConsAvg12</td>
                        <td style="color: red">@dto.ConsAvg12Value</td>
                        <td style="color: red">@dto.ConsAvg6</td>
                        <td style="color: red">@dto.ConsAvg3</td>

                        <td style="color: green">@dto.RecvFrequency</td>
                        <td style="color: green">@dto.RecvAvg12</td>
                        <td style="color: green">@dto.RecvAvg12Value</td>
                        <td style="color: green">@dto.RecvAvg6</td>
                        <td style="color: green">@dto.RecvAvg3</td>
                    }
                </tr>
                @if (_showMonthDetails)
                {
                    <tr>
                        <td colspan="12">
                            <table class="table table-hover table-striped">
                                <thead>
                                    <tr>
                                        @foreach (var t in dto.NavInvValuationDtos)
                                        {
                                            <td style="color: dodgerblue">@t.YearMonth.ToString("yyyy-MM")</td>
                                        }
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        @foreach (var t in dto.NavInvValuationDtos)
                                        {
                                            <td style="color: red">@t.DecreasesQuantity ( @t.DecreasesValue)</td>
                                        }
                                    </tr>
                                    <tr>
                                        @foreach (var t in dto.NavInvValuationDtos)
                                        {
                                            <td style="color: green">@t.IncreaseQuantity ( @t.IncreaseValue)</td>
                                        }
                                    </tr>
                                </tbody>
                            </table>
                        </td>
                    </tr>
                }
            }
        }
    </tbody>


</table>

@code {
    private string _search = string.Empty;
    private bool _valuation;
    private bool _showMonthDetails;
    public ObservableRangeCollection<NavMonthlyValuationDto> FsMonthlyValuationDtos { get; } = new();
    public ObservableRangeCollection<NavMonthlyValuationDto> FilterFsMonthlyValuationDtos { get; } = new();


    protected override async Task OnParametersSetAsync()
    {
        await GetData();
        Search(new ChangeEventArgs { Value = _search });
        StateHasChanged();
    }

    private void OnValuationChange(ChangeEventArgs e)
    {
        _valuation =Convert.ToBoolean(e.Value);
        StateHasChanged();
    }
    private void OnShowMonthDetailsChange(ChangeEventArgs e)
    {
        _showMonthDetails =Convert.ToBoolean(e.Value);
        StateHasChanged();
    }
    private async Task GetData()
    {
        FsMonthlyValuationDtos.ReplaceRange(await NavInvValuationService.GetMonthlyValuationsAsync());
        //异步方法需要主动告知前台状态已经发生改变
        StateHasChanged();
    }

    private void Search(ChangeEventArgs e)
    {
        //将搜索放在外面做（4.没搜索||5.搜索项目号||6.搜索项目名称）
        _search=e.Value!.ToString()!;

        FilterFsMonthlyValuationDtos.ReplaceRange(FsMonthlyValuationDtos.Where(
            x => string.IsNullOrEmpty(_search) ||
                 x.Number.ToLower().Contains(_search.ToLower()) ||
                 (!string.IsNullOrEmpty(x.Description) && x.Description.ToLower().Contains(_search.ToLower()))));

        StateHasChanged();
    }
}
